/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package construtora.limiar.dao;

import construtora.limiar.dao.generic.AbstractDAO;
import construtora.limiar.dao.generic.DAOEngine;
import construtora.limiar.dao.generic.IDAO;
import construtora.limiar.models.Endereco;
import construtora.limiar.models.generic.IModel;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author michel
 */
public class EnderecoDAO extends AbstractDAO implements IDAO{
    
    public EnderecoDAO() {
        this.table = "endereco";
    }
    
    public List<Endereco> fetchAll(String where, String order, Integer limit, Integer offset) {
        List<Endereco> list = new ArrayList<Endereco>();
        ResultSet rs = DAOEngine.fetchAll(this.table, where, order, limit, offset);
        try {
            while (rs.next()){
                Endereco endereco = new Endereco();
                endereco.setId(rs.getInt("id_endereco"));
                endereco.setRua(rs.getString("rua"));
                endereco.setNumero(rs.getInt("numero"));
                endereco.setComplemento(rs.getString("complemento"));
                endereco.setBairro(rs.getString("bairro"));
                endereco.setCidade(rs.getString("cidade"));
                endereco.setEstado(rs.getString("estado"));
                endereco.setCep(rs.getString("cep"));
                endereco.setStatus(rs.getString("status"));
                list.add(endereco);
            }
            return list;
        }catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    public IModel fetchOne(IModel model){
        String where = this.getDefaultWhere(model);
        List<Endereco> list = this.fetchAll(where, null, null, null);
        if (list != null && list.size() > 0)
            return list.get(0);
        else
            return null;
    }
    
    @Override
    public String[][] getFields(IModel model) {
        Endereco endereco = (Endereco) model;
        String[][] fields = new String[8][2];
        fields[0][0] = "rua";
        fields[0][1] = "'" + endereco.getRua() + "'";
        fields[1][0] = "numero";
        fields[1][1] = "'" + endereco.getNumero().toString() + "'";
        fields[2][0] = "complemento";
        fields[2][1] = "'" + endereco.getComplemento() + "'";        
        fields[3][0] = "bairro";
        fields[3][1] = "'" + endereco.getBairro() + "'";
        fields[4][0] = "cidade";
        fields[4][1] = "'" + endereco.getCidade() + "'";
        fields[5][0] = "estado";
        fields[5][1] = "'" + endereco.getEstado() + "'";
        fields[6][0] = "cep";
        fields[6][1] = "'" + endereco.getCep() + "'";
        fields[7][0] = "status";
        fields[7][1] = "'" + endereco.getStatus() + "'";
        
        return fields;    
    }
    
    
}
